package com.genesis.yunnanji.utils;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


/**
 * SharedPreference工具
 */
public class SharedPreferencesUtil {
    private static final String SHARE_FILE_NAME_MAIN = "rc_main";
    private static final String SHARE_FILE_NAME_RESOURCE = "rc_resource";
    private static final String SHARE_FILE_NAME_USER = "rc_user";
    public static final String SHARE_FILE_NAME_STICKER = "rc_sticker";
    public static final String SHARE_FILE_NAME_MUSIC_EDIT = "rc_music_edit";
    public static final String SHARE_FILE_NAME_MUSIC_RECORD = "rc_music_record";
    private SharedPreferences sp;

    private SharedPreferencesUtil(Context context, String shareName) {
        sp = context.getSharedPreferences(shareName, Context.MODE_PRIVATE);
    }

    public void clear() {
        Editor editor = sp.edit();
        editor.clear();
        editor.apply();
    }

    public void put(String key, int value) {
        Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public void put(String key, boolean value) {
        Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public void put(String key, float value) {
        Editor editor = sp.edit();
        editor.putFloat(key, value);
        editor.apply();
    }

    public void put(String key, long value) {
        Editor editor = sp.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public void put(String key, String value) {
        Editor editor = sp.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public void remove(String key) {
        Editor editor = sp.edit();
        editor.remove(key);
        editor.apply();
    }

    public int getInt(String key, int defValue) {
        int result = defValue;
        try {
            result = sp.getInt(key, defValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public boolean getBoolean(String key, boolean defValue) {
        boolean result = defValue;
        try {
            result = sp.getBoolean(key, defValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public float getFloat(String key, float defValue) {
        float result = defValue;
        try {
            result = sp.getFloat(key, defValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public long getLong(String key, long defValue) {
        long result = defValue;
        try {
            result = sp.getLong(key, defValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String getString(String key, String defValue) {
        String result = defValue;
        try {
            result = sp.getString(key, defValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取维护资源版本的 SharedPreferencesUtil
     *
     * @param context {@link Context}
     * @return {@link SharedPreferencesUtil}
     */
    public static SharedPreferencesUtil getResourceSharedPreferencesUtil(Context context) {
        return new SharedPreferencesUtil(context, SHARE_FILE_NAME_RESOURCE);
    }

    public static SharedPreferencesUtil getSharedPreferencesUtil(Context context, String shareName) {
        return new SharedPreferencesUtil(context, shareName);
    }

    public static SharedPreferencesUtil getMainSharedPreferencesUtil(Context context) {
        return new SharedPreferencesUtil(context, SHARE_FILE_NAME_MAIN);
    }

    public static SharedPreferencesUtil getUserMainSharedPreferencesUtil(Context context) {
        return new SharedPreferencesUtil(context, SHARE_FILE_NAME_USER);
    }

//    public static SharedPreferencesUtil getUserSharedPreferencesUtil(Context context) {
//        return new SharedPreferencesUtil(context, SHARE_FILE_NAME_USER + "_" + UserUtil.getId());
//    }

//    public static void saveUserList(List recordInfoList, Context context) {
//        SharedPreferencesUtil sp = getMainSharedPreferencesUtil(context);
//        try {
//            String list = SceneList2String(recordInfoList);
//            sp.put("listStr", list);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    public static List getUserList(Context context) {
//        List loginRecordInfoList = new ArrayList<>();
//        SharedPreferencesUtil sp = getMainSharedPreferencesUtil(context);
//        String list = sp.getString("listStr", "");
//        try {
//            loginRecordInfoList = String2SceneList(list);
//        } catch (ClassNotFoundException | IOException e) {
//            e.printStackTrace();
//        }
//        return loginRecordInfoList;
//    }
//
//    private static String SceneList2String(List SceneList)
//            throws IOException {
//        // 实例化一个ByteArrayOutputStream对象，用来装载压缩后的字节文件。
//        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//        // 然后将得到的字符数据装载到ObjectOutputStream
//        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
//                byteArrayOutputStream);
//        // writeObject 方法负责写入特定类的对象的状态，以便相应的 readObject 方法可以还原它
//        objectOutputStream.writeObject(SceneList);
//        // 最后，用Base64.encode将字节文件转换成Base64编码保存在String中
//        String SceneListString = new String(Base64.encode(
//                byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
//        // 关闭objectOutputStream
//        objectOutputStream.close();
//        return SceneListString;
//    }
//
//    private static List String2SceneList(String SceneListString) throws IOException,
//            ClassNotFoundException {
//        byte[] mobileBytes = Base64.decode(SceneListString.getBytes(),
//                Base64.DEFAULT);
//        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
//                mobileBytes);
//        ObjectInputStream objectInputStream = new ObjectInputStream(
//                byteArrayInputStream);
//        List SceneList = (List) objectInputStream
//                .readObject();
//        objectInputStream.close();
//        return SceneList;
//    }
}
